<template>
	<view class="tn-padding-sm content">
		<view class="box">
			<view class="title tn-text-lg">
				基本信息
			</view>
			<view class="tn-margin-top-sm content-from ">
				<view class="from tn-flex tn-flex-col-center tn-b-f  radius tn-padding-sm">
					<view class="left f-30 ">服务类型</view>
					<view class="right tn-margin-left-xs">
						<tn-input disabledColor="#fff" v-model="from.serviceType" placeholder="服务名称" 
							>
						</tn-input>
					</view>
				</view>
				<view class="from tn-margin-top-sm tn-flex tn-flex-col-center tn-b-f  radius tn-padding-sm">
					<view class="left f-30 ">服务名称</view>
					<view class="right tn-margin-left-xs">
						<tn-input v-model="from.serviceName" placeholder="服务名称" disabledColor="#fff" 
							>
						</tn-input>
					</view>
				</view>
			</view>
		</view>

		<view class="box tn-margin-top-xl">
			<view class="title tn-text-lg">
				联系人信息
			</view>
			<view class="tn-margin-top-sm content-from ">
				<view class="from tn-flex tn-flex-col-center tn-b-f  radius tn-padding-sm">
					<view class="left f-30 ">姓名</view>
					<view class="right tn-margin-left-xs">
						<tn-input v-model="from.applicant" placeholder="姓名" >
						</tn-input>
					</view>
				</view>
				<view class="from tn-margin-top-sm tn-flex tn-flex-col-center tn-b-f  radius tn-padding-sm">
					<view class="left f-30 ">手机号</view>
					<view class="right tn-margin-left-xs">
						<tn-input v-model="from.phone" placeholder="手机号" >
						</tn-input>
					</view>
				</view>
				<view class="from tn-margin-top-sm tn-flex tn-flex-col-center tn-b-f  radius tn-padding-sm">
					<view class="left f-30 ">邮箱</view>
					<view class="right tn-margin-left-xs">
						<tn-input v-model="from.email" placeholder="邮箱" >
						</tn-input>
					</view>
				</view>
				<view class="from tn-margin-top-sm tn-flex tn-flex-col-center tn-b-f  radius tn-padding-sm">
					<view class="left f-30 ">公司名称</view>
					<view class="right tn-margin-left-xs">
						<tn-input v-model="from.companyName" placeholder="公司名称">
						</tn-input>
					</view>
				</view>
			</view>
		</view>
		<view class="footer tn-padding-sm  tn-margin-top-sm">
			<view class="btn" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	import {
		GetenterpriseId,
		GetenterpriseHelpCategoryId,
		signUp
	} from '@/api/enterprise/enterprise.js'
	import {
		formatTime
	} from '@/utils/date.js'
	export default {
		data() {
			return {
				from: {
					serviceType: '',
					serviceName: '',
					applicant: '',
					phone: '',
					email: '',
					companyName: ''
				},
				cateid: '',
				id: ""
			}
		},
		onLoad(options) {
			this.id = parseInt(options.leftid);
			this.cateid = parseInt(options.cateid)
		},
		onShow() {
			 this.getenterpriseId()
			 this.getenterpriseHelpCategoryById()
		},
		methods: {
			getenterpriseId() {
				let param = {
					id: this.id,

				}
				GetenterpriseId(param).then((res) => {
					this.from.serviceName = res.data.name
				})
			},
			getenterpriseHelpCategoryById() {
				let param = {
					id: this.cateid,

				}
				GetenterpriseHelpCategoryId(param).then((res) => {
					this.from.serviceType = res.data.name
				})
			},
			submit() {
				let from = this.from
				if (!from.applicant) return uni.showToast({
					title: "姓名不能为空",
					icon: 'none',
					duration: 2000
				})
				if (!from.phone) return uni.showToast({
					title: "邮箱不能为空",
					icon: 'none',
					duration: 2000
				})
				let flag = this.validatePhoneNumber(from.phone)
				if (!flag) return uni.showToast({
					title: "手机验证失败",
					icon: 'none',
					duration: 2000
				})
				let param = {
					...from,
					registrationTime: formatTime(new Date()),
					state: '0'
				}
				uni.showLoading({
					title: '提交中'
				})
				signUp(param).then(() => {
					uni.hideLoading();
					uni.showToast({
						title: "提交成功",
						icon: 'none',
						duration: 2000
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1000);
				}, error => {
					uni.hideLoading();
				})
			},
			validatePhoneNumber(phoneNumber) {
				const phoneRegex = /^1[3-9]\d{9}$/;
				return phoneRegex.test(phoneNumber);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		.title {
			font-weight: 400;
		}

		.content-from {
			.from {
				.left {
					line-height: 48rpx;
					color: #191919;
				}
			}

		}
	}

	.footer {
		.btn {
			margin: auto;
			line-height: 60rpx;
			padding: 10rpx 0;
			border-radius: 20rpx;
			background-color: #4152eb;
			color: #fff;
			font-tn-text-md: 32rpx;
			text-align: center;
		}
	}
</style>